package com.xinlang.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 */
@Deprecated
public class DateUtil {

	static SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	// ��õ�ǰ��ʱ��
	static public String getCurrentDatetime() {
		return datetimeFormat.format(new Date());
	}

	/**
	 * ��õ�ǰ����
	 */
	static public String getCurrentDate() {
		return dateFormat.format(new Date());
	}

	/**
	 * ��Dateת��"yyyy-MM-dd HH:mm:ss"�ַ�
	 * 
	 * @param date
	 * @return
	 */
	static public String datetimeToString(Date date) {
		return datetimeFormat.format(date);
	}

	/**
	 * ��Dateת��"yyyy-MM-dd"�ַ�
	 * 
	 * @param date
	 * @return
	 */
	static public String dateToString(Date date) {
		return dateFormat.format(date);
	}

	@Deprecated
	/**
	 * �Ƽ��ĳ� parseDatetime()��parseDate()
	 */
	public static Date stringToDate(String strDate) {
		if (strDate == null)
			return null;
		Date date = null;
		try {
			date = datetimeFormat.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * ��"yyyy-MM-dd HH:mm:ss"ʱ���ַ������Date��ʽ���
	 * 
	 * @param strDate
	 * @return
	 */
	public static Date parseDatetime(String strDate) {
		if (strDate == null)
			return null;
		Date date = null;
		try {
			date = datetimeFormat.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * ��"yyyy-MM-dd"�����ַ������Date��ʽ���
	 * 
	 * @param strDate
	 * @return
	 */
	public static Date parseDate(String strDate) {
		if (strDate == null)
			return null;
		Date date = null;
		try {
			date = dateFormat.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * ��ý�������
	 * 
	 * @return
	 */
	static public String getTodayDate() {
		return getCurrentDate();
	}

	/**
	 * ��ȡ��ǰ��datetime
	 * 
	 * @return
	 */
	static public String now() {
		return getCurrentDatetime();
	}

	/**
	 * ���Unixʱ���(��1970-1-1(UTC/GMT��ҹ)��ʼ���������������롣
	 * 
	 * @return
	 */
	static public long getUnixTimestamp() {
		long epoch = System.currentTimeMillis() / 1000;
		return epoch;
	}

	/**
	 * ��ָ������ת����ʱ���
	 */
	static public long getUnixTimestamp(String date) {
		Date epoch;
		long timestamp = 0;
		try {
			epoch = datetimeFormat.parse(date);
			timestamp = epoch.getTime() / 1000;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return timestamp;
	}

	/**
	 * ����ǵ�ʱ���
	 */
	static public long differenceInSecond(String date1, String date2) {
		long second1 = getUnixTimestamp(date1);
		long second2 = getUnixTimestamp(date2);
		return second1 - second2;
	}

	static public long differenceInSecond(Date parseDate1, Date parseDate2) {
		// ��������õ���������
		long diff = 0;
		diff = (parseDate1.getTime() - parseDate2.getTime()) / (1000);
		return diff;
	}

	/**
	 * �Է��Ӽǵ�ʱ���
	 */
	static public long differenceInMinute(Date date1, Date date2) {
		// ��������õ���������
		long diff = 0;
		diff = (date1.getTime() - date2.getTime()) / (60 * 1000);
		return diff;
	}

	/**
	 * ����ǵ�ʱ���
	 */
	static public long differenceInDay(String date1, String date2) {
		long second1 = getUnixTimestamp(date1);
		long second2 = getUnixTimestamp(date2);
		return (second1 - second2) / (3600 * 24);
	}

	/**
	 * ����ǵ�ʱ���
	 */
	static public long differenceInDay(Date date1, Date date2) throws ParseException {
		// ��������õ���������
		long day = 0;
		day = (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000);
		return day;
	}

	@Deprecated
	/**
	 * ��getDateAfterDays()���
	 * @param d
	 * @param day
	 * @return
	 */
	public static Date getDateAfter(Date d, int day) {
		return getDateAfterDays(d, day);
	}

	/**
	 * �õ�n����ʱ��
	 */
	public static Date getDateAfterDays(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		return now.getTime();
	}

	/**
	 * ��ȡ�Խ���֮��n���ʱ��
	 */
	public static Date getDateAfterDays(int day) {
		return getDateAfterDays(new Date(), day);
	}

	/**
	 * �õ�n��Сʱ֮���ʱ��
	 * 
	 * @param d
	 * @param hour
	 * @return
	 */
	public static Date getDateAfterHours(Date d, int hour) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.HOUR, now.get(Calendar.HOUR) + hour);
		return now.getTime();
	}

	public static Date getDateAfterHours(int hour) {
		return getDateAfterHours(new Date(), hour);
	}

	/**
	 * ��ȡn����֮���ʱ��
	 * 
	 * @param d
	 * @param minutes
	 * @return
	 */
	public static Date getDateAfterMinutes(Date d, int minutes) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + minutes);
		return now.getTime();
	}

	@Deprecated
	// �� getDateAfterMinutes()���
	public static Date getDatetimeAfterMinutes(Date d, int minutes) {
		return getDateAfterMinutes(d, minutes);
	}

	/**
	 * ��ȡn��֮���ʱ��
	 * 
	 * @param seconds
	 * @return
	 */
	public static Date getDateAfterSeconds(Date date, int seconds) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.set(Calendar.SECOND, now.get(Calendar.SECOND) + seconds);
		return now.getTime();
	}

	public static Date getDateAfterSeconds(int seconds) {
		return getDateAfterSeconds(new Date(), seconds);
	}

	@Deprecated
	// ��getDateAfterSeconds()���
	public static Date getDatetimeAfterSeconds(int seconds) {
		return getDateAfterSeconds(new Date(), seconds);
	}

	// ���timestamp���Date
	public static Date getDate(long timestamp) {
		return new Date(timestamp * 1000);
	}

	/**
	 * ��getDate()���
	 * 
	 * @param timestamp
	 * @return
	 */
	@Deprecated
	public static Date getDateFromTimestamp(long timestamp) {
		Date date = new Date(timestamp * 1000);
		return date;
	}

	static public void main(String[] args) {
		/*
		 * System.out.println(differenceInSecond("2010-09-20 19:40:52",
		 * "2010-09-20 19:40:58")); System.out.println(getCurrentDatetime());
		 * 
		 * System.out.println(getDateAfterDays(1));
		 */

		// System.out.println(getUnixTimestamp("2011-01-12 11:22:00"));
		System.out.println(datetimeToString(getDate(1274324014L)));

		// // 2011-01-12
		System.out.println(DateUtil.getTodayDate());
		// 11:22
	}
}
